package com.irs.irsforum.web.common;


import com.alibaba.rocketmq.shade.com.alibaba.fastjson.JSONObject;
import com.irs.irsforum.utils.JsonUtil;

import java.io.IOException;
import java.util.List;

/**
 * @author yihuili
 *
 * @since 2020-01-18
 * */
public class ResponseResult<T> {

    private static final int SUCCESS = 0;
    private static final int FAILURE = 1;

    private T data;

    @Deprecated
    private int code;

    private int status;
    private String msg;

    public static final String DEFAULT_SUCCESS_MESSAGE = "success";

    public static final String DEFAULT_FAIL_MESSAGE = "Operation failed";

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public JSONObject json() {
        JSONObject json = new JSONObject();
        json.put("code", status);
        json.put("msg", msg);
        return json;
    }

    public static <T> ResponseResult<T> newSuccessResult(T data) {
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setData(data);
        responseResult.setStatus(SUCCESS);
        responseResult.setCode(SUCCESS);
        responseResult.setMsg(DEFAULT_SUCCESS_MESSAGE);

        return responseResult;
    }

    public static ResponseResult newSuccessResult() {
        ResponseResult responseResult = new ResponseResult();
        responseResult.setStatus(SUCCESS);
        responseResult.setCode(SUCCESS);
        responseResult.setMsg(DEFAULT_SUCCESS_MESSAGE);

        return responseResult;
    }

    public static <T> ResponseResult<T> newFailResult(T data) {
        ResponseResult<T> responseResult = new ResponseResult<T>();
        responseResult.setData(data);
        responseResult.setStatus(FAILURE);
        responseResult.setCode(FAILURE);
        responseResult.setMsg(DEFAULT_FAIL_MESSAGE);
        return responseResult;
    }

    public static ResponseResult newFailResult(int status, String msg) {
        ResponseResult responseResult = new ResponseResult();
        responseResult.setStatus(FAILURE);
        responseResult.setCode(FAILURE);
        responseResult.setMsg(msg);
        return responseResult;
    }

    public static <T> ResponseResult<T> newFailResult(T data, int status, String msg) {
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setData(data);
        responseResult.setStatus(status);
        responseResult.setCode(status);
        responseResult.setMsg(msg);
        return responseResult;
    }

    public static <T> T getData(ResponseResult responseResult, Class<T> clazz) throws IOException {
        if(null == responseResult || null == responseResult.getData()) {
            return null;
        }

        return JsonUtil.deserialze(JsonUtil.toJsonString(responseResult.getData()), clazz);
    }

    public static <T> List<T> getList(ResponseResult responseResult, Class<T> clazz) throws IOException {
        if(null == responseResult || null == responseResult.getData()) {
            return null;
        }

        return JsonUtil.deserialzeList(JsonUtil.toJsonString(responseResult.getData()), clazz);
    }
}
